﻿using System;
namespace Velocity4Net.Runtime.Parser.Node{









public class ASTNegateNode:SimpleNode
{
    protected bool strictMode = false;

    public ASTNegateNode(int i)
    {
        super(i);
    }

    public ASTNegateNode(Parser p, int i)
    {
        super(p, i);
    }

    /**
     * {@inheritDoc}
     */
    public Object init(InternalContextAdapter context, Object data) {
        super.init(context, data);
        /* save a literal image now (needed in case of error) */
        strictMode = rsvc.getBoolean(RuntimeConstants.STRICT_MATH, false);
        cleanupParserAndTokens();
        return data;
    }

    /**
     * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object)
     */
    public Object jjtAccept(ParserVisitor visitor, Object data)
    {
        return visitor.visit(this, data);
    }

    /**
     * @see org.apache.velocity.runtime.parser.node.SimpleNode#evaluate(org.apache.velocity.context.InternalContextAdapter)
     */
    public bool evaluate( InternalContextAdapter context)
            {
        return jjtGetChild(0).evaluate(context);
    }

    /**
     * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter)
     */
    public Object value( InternalContextAdapter context)
            {
        Object value = jjtGetChild(0).value( context );
        try
        {
            value = DuckType.asNumber(value);
        }
        catch (NumberFormatException nfe) {}
        if (!(value is Number))
        {
            String msg = "Argument of unary negate (" +
                    jjtGetChild(0).literal() +
                    ") " +
                    (value == null ? "has a null value." : "is not a Number.");
            if (strictMode)
            {
                log.Error(msg);
                throw new MathException(msg, rsvc.getLogContext().getStackTrace());
            }
            else
            {
                log.Debug(msg);
                return null;
            }
        }
        return MathUtils.negate((Number) value);
    }
    
    public override String literal()
    {
        return "-" + jjtGetChild(0).literal();
    }
}
}